package com.manager.action.news;

import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.common.tool.FillBeanUtils;
import org.common.util.TranslateHelper;
import org.wface.web.core.ActionSupport;

import com.manager.service.news.CategoryService;
import com.manager.service.news.NewsService;
import com.web.model.Category;
import com.web.model.News;
import com.web.model.vo.query.NewsQuery;
import com.web.tag.bean.PageModel;

public class NewsAction extends ActionSupport {

	private String id;
	private String page;

	private String classid;
	private String title;

	public String list() {
		PageModel model = new PageModel();
		int pageNum = TranslateHelper.parseInteger(page);
		model.setPageNum(pageNum);

		CategoryService cateService = new CategoryService();
		List<Category> categorys = cateService.findList();
		getRequest().setAttribute("categorys", categorys);

		NewsService newsService = new NewsService();
		NewsQuery query = new NewsQuery();
		query.setClassid(TranslateHelper.parseInteger(classid));
		query.setTitle(title);
		List<News> list = newsService.findList(model, query);
		getRequest().setAttribute("newsList", list);
		getRequest().setAttribute("query", query);

		model.setPaths(getBasePath() + "/manager/news/list.do?");
		getRequest().setAttribute(PageModel.PAGEMODEL, model);
		return "list";
	}

	private String getBasePath() {
		String basePath = getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort() + getRequest().getContextPath();
		return basePath;
	}

	public String create() {
		CategoryService cateService = new CategoryService();
		List<Category> categorys = cateService.findList();
		getRequest().setAttribute("categorys", categorys);
		return "create";
	}

	public String save() {
		NewsService newsService = new NewsService();
		News news = new News();
		String contentHtml = getRequest().getParameter("contentHtml");
		
		if(getTime(getRequest().getParameter("addtime"))!=null){
		  news.setAddtime(getTime(getRequest().getParameter("addtime")));
		}
		// newsService.
		Map<String, String[]> parameterMap = getRequest().getParameterMap();

		new FillBeanUtils(parameterMap).injectBean(news);
		/*
		 * if(contentHtml!=null){ try { news.setContentHtml(new
		 * javax.sql.rowset.serial.SerialClob(contentHtml.toCharArray())); }
		 * catch (SerialException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); } catch (SQLException e) { // TODO
		 * Auto-generated catch block e.printStackTrace(); } }
		 */
		newsService.saveOrUpdate(news, contentHtml);
		return "!/manager/news/list.do";
	}

	public String edit() {
		NewsService newsService = new NewsService();
		News obj = newsService.getById(TranslateHelper.parseInteger(id));
		Clob clob = obj.getContentHtml();
		StringBuffer content = new StringBuffer();
		content.append("");
		Reader reader = null;

		try {
			reader = clob.getCharacterStream();

			int i = 0;
			char ac[] = new char[200];
			while ((i = reader.read(ac, 0, 200)) != -1)
				content.append(new String(ac, 0, i));

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		getRequest().setAttribute("news", obj);
		getRequest().setAttribute("content", content.toString());
		return create();
	}

	public String delete() {
		NewsService newsService = new NewsService();
		newsService.deleteNews(TranslateHelper.parseInteger(id));
		return "!/manager/news/list.do";
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public void setClassid(String classid) {
		this.classid = classid;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	public Timestamp getTime(String str_test){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		  format.setLenient(false);
		  //要转换字符串 str_test
		  Timestamp ts =null;
		  try {
		   ts = new Timestamp(format.parse(str_test).getTime());
		   System.out.println(ts.toString());
		  } catch (ParseException e) {
		   // TODO Auto-generated catch block
		   e.printStackTrace();
		  }
		  return ts;
		  
	}

}
